﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeatherStationSys.Interface;

namespace WeatherMonitoringSystem
{
    public class HiloDataImp : IHiLoData
    {
        private IPersistent _iPersistent;
        private string _type;
        private string _storageKey;
        private long _heightTime;
        private double _heightValue;
        private long _lowerTime;
        private double _lowerValue;

        /// <summary>
        /// Ctor
        /// </summary>
        public HiloDataImp(StationTookit st, string type, DateTime theDate, double init, long initTime)
        {
            _iPersistent = st.GetPersistent();
            _type = type;
            _storageKey = CalculateStorageKey(theDate);
            IHiLoData iHiLoData = _iPersistent.Retrieve<IHiLoData>(_storageKey);
            _heightTime = iHiLoData.GetHeightTime();
            _heightValue = iHiLoData.GetHeightValue();
            _lowerTime = iHiLoData.GetLowerTime();
            _lowerValue = iHiLoData.GetLowerValue();
            CurrentReading(init, initTime);
        }

        /// <summary>
        /// 当前温度
        /// </summary>
        /// <param name="value">当前值</param>
        /// <param name="time">当前时间</param>
        public bool CurrentReading(double currentValue, long time)
        {
            bool isChanged = false;
            if (currentValue > _heightValue)
            {
                _heightValue = currentValue;
                _heightTime = time;
                isChanged = true;
            }
            else if (currentValue < _lowerValue)
            {
                _lowerValue = currentValue;
                _lowerTime = time;
                isChanged = true;
            }

            return isChanged;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string CalculateStorageKey(DateTime theDate)
        {
            string key = _type + theDate.ToString("yyyyMMdd");
            return key;
        }

        /// <summary>
        /// 新的1天
        /// </summary>
        /// <param name="initial">初始值</param>
        /// <param name="time">当前时间</param>
        public void NewDay(double initial, long time)
        {
            _lowerValue = _heightValue = initial;
            _lowerTime = _heightTime = time;
        }

        /// <summary>
        /// 得到最大值
        /// </summary>
        /// <returns></returns>
        public double GetHeightValue() => _heightValue;


        /// <summary>
        /// 得到最大值的时间
        /// </summary>
        /// <returns></returns>
        public long GetHeightTime() => _heightTime;

        /// <summary>
        /// 得到最小值的时间
        /// </summary>
        /// <returns></returns>
        public long GetLowerTime() => _lowerTime;

        /// <summary>
        /// 得到最小值
        /// </summary>
        /// <returns></returns>
        public double GetLowerValue() => _lowerValue;

        /// <summary>
        /// 老版本持久化数据
        /// </summary>
        /// <param name="value">当前值</param>
        /// <param name="time">当前时间</param>
        public void DeformedVeiosnCurrentReading(double currentValue, long time)
        {
            throw new NotImplementedException();
        }
    }
}
